public class Solution6 {
    public String convert(String s, int numRows) {
        if(numRows == 1) return s;
        StringBuilder[] arr = new StringBuilder[numRows];
        for(int i = 0; i < numRows; i++) {
            arr[i] = new StringBuilder();
        }
        int idx = 0;
        int row = 0;
        boolean flag = true;
        while(idx < s.length()) {
            arr[row].append(s.charAt(idx));
            if(flag && row == numRows - 1) {
                row--;
                flag = false;
            } else if(!flag && row == 0){
                row++;
                flag = true;
            }else if(flag) {
                row++;
            }else {
                row--;
            }
            idx++;
        }
        StringBuilder res = new StringBuilder();
        for(int i = 0; i < numRows; i++) {
            res.append(arr[i]);
        }
        return res.toString();
    }
}
